cap program drop StateIDs
program define StateIDs
syntax using/, HAVE(string) WANT(string) [keepmatching]

	cap confirm variable `want'
	if _rc==0 {
		noi di in red "  Variable `want' already exists in data."
		exit 1
	}
	confirm variable `have'

	tempfile tmp
	qui save `tmp'

	clear
	qui import delim `using', varn(1) stringcol(_all) 
	keep `have' `want'
	isid `have'
	tempfile xwalk
	qui save `xwalk'

	use `tmp'
	macro drop _tmp

	sort `have'
	cap isid `have'
	if _rc==0 {
		noi di in green _n "  Performing " in ye "1:1 " in green "merge on `have', adding " in ye "`want'" in green "." _n
		qui merge 1:1 `have' using `xwalk'
	}
	else {
		noi di in green _n "  Performing " in ye "m:1 " in green "merge on `have', adding " in ye "`want'" in green "." _n
		qui merge m:1 `have' using `xwalk'
	}
	macro drop _xwalk

	qui count if _merge==3
	if `r(N)'==`=_N' {
		noi di in green "  Perfect merge, dropping _merge variable." _cont
		drop _merge
	}
	else {
		noi di in yellow "  At least one mismatched observations."
		tab _merge, m
		if "`keepmatching'"=="keepmatching" {
			noi di in yellow _n "  Keeping matching observations (_merge==3) only. " _cont
			keep if _merge==3
			drop _merge
		}
	}

end
